package com.crm.service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.crm.dao.Fp2DAO;
import com.crm.po.Fp2;
import com.crm.util.DateEx;
import com.crm.util.ExcelEx;
import com.crm.util.MathEx;
import com.crm.util.RequestEx;
import com.crm.util.StringEx;
import com.crm.vo.Excel;

public class Fp2Service {
	
	private static Logger logger = Logger.getLogger(Fp2Service.class);
	
	public Map<String, Object> getFp2s(HttpServletRequest request){
		StringBuffer Hql = new StringBuffer(" from Fp2 t where 1=1 ");
		int page = Integer.parseInt(request.getParameter("page"));//当前第几页
		int rows = Integer.parseInt(request.getParameter("rows"));//每页显示几行
		
		String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
		String islive = RequestEx.getStringTrim(request.getParameter("islive"));
		String remitcompany = RequestEx.getStringTrim(request.getParameter("remitcompany"));
		String content = RequestEx.getStringTrim(request.getParameter("content"));
		String recnumber = RequestEx.getStringTrim(request.getParameter("recnumber"));
		String fromDate = RequestEx.getStringTrim(request.getParameter("fromDate"));
		String toDate = RequestEx.getStringTrim(request.getParameter("toDate"));
		
		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		if(remitcompany != null && remitcompany.trim().length() > 0){
			Hql.append(" and t.remitcompany like '%");
			Hql.append(remitcompany);
			Hql.append("%' ");
		}
		if(content != null && content.trim().length() > 0){
			Hql.append(" and t.content like '%");
			Hql.append(content);
			Hql.append("%' ");
		}
		if(recnumber != null && recnumber.trim().length() > 0){
			Hql.append(" and t.recnumber like '%");
			Hql.append(recnumber);
			Hql.append("%' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() == 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '9999-12-31' ");
		}
		if(fromDate.trim().length() == 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '1970-01-01' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		
		Fp2DAO dao = new Fp2DAO();
		String countHql = "select count(t.recid) " + Hql.toString();
		int count = dao.getCount(countHql);
		Hql.append(" order by t.remitdate desc, t.creattime desc  ");
	    List<Fp2> list = dao.fp2ListPage(Hql.toString(), (page-1) * rows, rows);
	    
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("count", count);
	    map.put("rows", list);
	    
	    return map;
	} 
	
	public int getFp2CountByTreeid(String treeid, String islive){
		StringBuffer Hql = new StringBuffer(" from Fp2 t where 1=1 ");

		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		
		Fp2DAO dao = new Fp2DAO();
		String countHql = "select count(t.recid) " + Hql.toString();
		int count = dao.getCount(countHql);
	    
	    return count;
	} 
	
	public boolean saveOrUpdateFp2(HttpServletRequest request){
		try{
			
			String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
			
			String recid = RequestEx.getStringTrim(request.getParameter("recid"));
			String name = RequestEx.getStringTrim(request.getParameter("name"));
			Date remitdate = RequestEx.getSqlDate(request.getParameter("remitdate"));
			String remitcompany = RequestEx.getStringTrim(request.getParameter("remitcompany"));
			Double price = RequestEx.getDouble(request.getParameter("price"));
			String content = RequestEx.getStringTrim(request.getParameter("content"));
			String recnumber = RequestEx.getStringTrim(request.getParameter("recnumber"));
			String remark = RequestEx.getStringTrim(request.getParameter("remark"));
			
			Fp2DAO dao = new Fp2DAO();
			Fp2 fp2 = null;
			if(recid != null && recid.trim().length() > 0){
				fp2 = dao.findById(recid);
			}else{				
				fp2 = new Fp2();
				fp2.setCreattime(DateEx.getCurDatetimeAsLong());
				fp2.setCreater(RequestEx.getCurUser(request).getUsername());
			}
			fp2.setName(name);
			fp2.setRemitdate(remitdate);
			fp2.setRemitcompany(remitcompany);
			fp2.setPrice(price);
			fp2.setContent(content);
			fp2.setRecnumber(recnumber);
			fp2.setCGroup(treeid);
			fp2.setRemark(remark);
			fp2.setIslive("Y");
			
			fp2.setUpdatetime(DateEx.getCurDatetimeAsLong());
			fp2.setUpdater(RequestEx.getCurUser(request).getUsername());
			
			dao.saveOrUpdate(fp2);
		}catch(Exception e){
			logger.error("save or update fp2 fail:", e);
			return false;
		}
	    return true;
	}
	
	public boolean deleteFp2s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "UPDATE Fp2 T SET T.islive = 'N' WHERE T.recid IN (" + recids + ")";
				Fp2DAO dao = new Fp2DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("delete fp2 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public boolean recoverFp2s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "UPDATE Fp2 T SET T.islive = 'Y' WHERE T.recid IN (" + recids + ")";
				Fp2DAO dao = new Fp2DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("recover fp2 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public boolean reallyDeleteFp2s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "DELETE FROM Fp2 T WHERE T.recid IN (" + recids + ")";
				Fp2DAO dao = new Fp2DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("really delete fp2 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public double mathJJCC(HttpServletRequest request){
		int type = RequestEx.getInteger(request.getParameter("type"));
		Double number1 = RequestEx.getDouble(request.getParameter("n1"));
		Double number2 = RequestEx.getDouble(request.getParameter("n2"));
		
		return MathEx.mathJJCC(number1, number2, 2, type);
	}
	
	public String mathBatchAdd(HttpServletRequest request){			
		String price_temp = RequestEx.getStringTrim(request.getParameter("price_temp"));
		double gprice_sum = MathEx.mathBatchAdd(price_temp.split("_"), 2);
		
		StringBuffer buffer = new StringBuffer("");
		buffer.append("{\"price_sum\":\"" + gprice_sum + "\"}");

		return buffer.toString();
	}


	public void exportFp2s(HttpServletRequest request, HttpServletResponse response){
		StringBuffer Hql = new StringBuffer(" from Fp2 t where 1=1 ");
		
		String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
		String islive = RequestEx.getStringTrim(request.getParameter("islive"));
		String recids = RequestEx.getStringTrim(request.getParameter("recids"));
		String remitcompany = RequestEx.getStringTrim(request.getParameter("remitcompany"));
		String content = RequestEx.getStringTrim(request.getParameter("content"));
		String recnumber = RequestEx.getStringTrim(request.getParameter("recnumber"));
		String fromDate = RequestEx.getStringTrim(request.getParameter("fromDate"));
		String toDate = RequestEx.getStringTrim(request.getParameter("toDate"));
		
		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		if(recids != null && recids.trim().length() > 0){
			Hql.append(" and t.recid in (");
			Hql.append(recids);
			Hql.append(") ");
		}
		if(remitcompany != null && remitcompany.trim().length() > 0){
			Hql.append(" and t.remitcompany like '%");
			Hql.append(remitcompany);
			Hql.append("%' ");
		}
		if(content != null && content.trim().length() > 0){
			Hql.append(" and t.content like '%");
			Hql.append(content);
			Hql.append("%' ");
		}
		if(recnumber != null && recnumber.trim().length() > 0){
			Hql.append(" and t.recnumber like '%");
			Hql.append(recnumber);
			Hql.append("%' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() == 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '9999-12-31' ");
		}
		if(fromDate.trim().length() == 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '1970-01-01' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		
		Fp2DAO dao = new Fp2DAO();
		Hql.append(" order by t.remitdate desc ");
	    List<Fp2> list = dao.fp2List(Hql.toString());
	    
	    String[][] grids = new String[list.size() + 1][9];
	    String[] titles = {"名称", "开票日期", "开票单位 ", "金额 ", "内容", "发票号", "备注", "创建时间", "修改时间"}; 
	    grids[0] = titles;
		for(int i = 0; i < list.size(); i ++){
			Fp2 fp2 = list.get(i);
			grids[i + 1][0] = StringEx.valueOf(fp2.getName());
			grids[i + 1][1] = StringEx.valueOf(fp2.getRemitdate(), StringEx.formatDate);
			grids[i + 1][2] = StringEx.valueOf(fp2.getRemitcompany());
			grids[i + 1][3] = StringEx.valueOf(fp2.getPrice());
			grids[i + 1][4] = StringEx.valueOf(fp2.getContent());
			grids[i + 1][5] = StringEx.valueOf(fp2.getRecnumber());
			grids[i + 1][6] = StringEx.valueOf(fp2.getRemark());
			grids[i + 1][7] = StringEx.valueOfLongDate(fp2.getCreattime());
			grids[i + 1][8] = StringEx.valueOfLongDate(fp2.getUpdatetime());
		}
	    ExcelEx.toExcel(request, response, new Excel("发票信息[导出时间" + DateEx.getCurDateTime() + "]", grids));
	} 
	
	public static void main(String[] args){

	}
	
}
